package com.realbrand.net.realbrandpay.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.realbrand.net.realbrandpay.R;
import com.realbrand.net.realbrandpay.bean.ConfirmOrderBean;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by Administrator on 2018/5/17.
 */

public class ConfirmOrderAdapter extends BaseAdapter {
    private List<ConfirmOrderBean> list;
    private Context mContext;
    private LayoutInflater inflater;

    public ConfirmOrderAdapter(List<ConfirmOrderBean> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.confirmorder_lv_item_layout, parent, false);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Picasso.with(mContext).load(list.get(position).getBrandLog()).into(viewHolder.confirmOrder_lv_item_iv_brandLog);
        viewHolder.confirmOrder_lv_item_tv_brandName.setText(list.get(position).getBrandName());
        String[] split = list.get(position).getCommodityLog().split(",");
        Picasso.with(mContext).load(split[0]+split[1]).into(viewHolder.confirmOrder_lv_item_iv_commodityLog);
        viewHolder.confirmOrder_lv_item_tv_commodityName.setText(list.get(position).getCommodityName());
        viewHolder.confirmOrder_lv_item_vv_commodityPrice.setText(list.get(position).getCommodityPricel());
        viewHolder.confirmOrder_lv_item_tv_commoditySum.setText("x" + list.get(position).getCommoditySum());
        if (position > 0) {
            if (list.get(position).getBrandName().equals(list.get(position - 1).getBrandName())) {
                viewHolder.confirmOrder_lv_item_iv_brandLog.setVisibility(View.GONE);
                viewHolder.confirmOrder_lv_item_tv_brandName.setVisibility(View.GONE);
            }
        }
        return convertView;
    }

    class ViewHolder {
        private ImageView confirmOrder_lv_item_iv_brandLog;
        private TextView confirmOrder_lv_item_tv_brandName;
        private ImageView confirmOrder_lv_item_iv_commodityLog;
        private TextView confirmOrder_lv_item_tv_commodityName;
        private TextView confirmOrder_lv_item_vv_commodityPrice;
        private TextView confirmOrder_lv_item_tv_commoditySum;

        public ViewHolder(View view) {
            confirmOrder_lv_item_iv_brandLog = (ImageView) view.findViewById(R.id.confirmorder_lv_item_iv_brand);
            confirmOrder_lv_item_tv_brandName = (TextView) view.findViewById(R.id.confirmorder_lv_item_tv_brandName);
            confirmOrder_lv_item_iv_commodityLog = (ImageView) view.findViewById(R.id.confirmorder_lv_item_iv_commodity);
            confirmOrder_lv_item_tv_commodityName = (TextView) view.findViewById(R.id.confirmorder_lv_item_tv_commodityName);
            confirmOrder_lv_item_vv_commodityPrice = (TextView) view.findViewById(R.id.confirmorder_lv_item_tv_money);
            confirmOrder_lv_item_tv_commoditySum = (TextView) view.findViewById(R.id.confirmorder_lv_item_tv_sum);
        }

    }

}
